package com.exsun.ex1077.ui.popup

import android.content.Context
import com.exsun.ex1077.R
import com.exsun.ex1077.databinding.PopupMsgBinding
import com.zhangping.fastjetpack.binding.txColor
import com.zhangping.fastjetpack.extend.view.toBinding
import com.zhangping.fastjetpack.extend.dp2px
import com.zhangping.fastjetpack.extend.screenWidth
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.PositionPopupView
import com.lxj.xpopup.enums.PopupAnimation

/**
 * 描述：消息提示
 * 作者：ZhangPing
 * 创建时间：2022/5/20 15:38
 */
class MsgPopup(context: Context, private val icon: Int, private val msg: String,
               private val textColor: Int, private val delay: Long): PositionPopupView(context) {

    override fun getImplLayoutId() = R.layout.popup_msg

    private lateinit var binding: PopupMsgBinding

    override fun onCreate() {
        super.onCreate()
        binding = popupImplView.toBinding()
        binding.icon.setImageResource(icon)
        binding.text.text = msg
        binding.text.txColor(textColor)
        delayDismiss(delay)
    }

    companion object{
        fun success(context: Context, msg: String){
            showPopup(context, R.drawable.msg_success, msg)
        }

        fun error(context: Context, msg: String){
            showPopup(context, R.drawable.msg_fail, msg, R.color.color_E34D59)
        }

        private fun showPopup(context: Context, iconId: Int = R.drawable.msg_success, msg: String = "操作成功",
                              textColor: Int = R.color.color_00A870, delay: Long = 2000){
            XPopup.Builder(context)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isCenterHorizontal(true)
                .hasShadowBg(false)
                .isLightStatusBar(true)
                .isCoverSoftInput(true)
                .offsetY(dp2px(100))
                .asCustom(MsgPopup(context, iconId, msg, textColor, delay))
                .show()
        }
    }

    override fun getPopupWidth(): Int {
        return (screenWidth() * 0.8F).toInt()
    }

}